package com.jy.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.teaopenapi.models.Config;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.user.param.DeleteFileByOssUrl;
import com.jy.entity.Alioss;
import com.jy.mapper.AliossMapper;
import com.jy.service.AliossService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * <p>
 * 阿里云配置信息表 服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-11-22
 */
@Service
public class AliossServiceImpl extends ServiceImpl<AliossMapper, Alioss> implements AliossService {

    private final AliossMapper aliossMapper;

    public AliossServiceImpl(AliossMapper aliossMapper) {
        this.aliossMapper = aliossMapper;
    }

    @Override
    public ResponseWrapper deleteFileByOssUrl(DeleteFileByOssUrl param) {

        // 从URL中提取文件名
        String fileName = param.getUrl().substring(param.getUrl().lastIndexOf("/") + 1);
        Alioss alioss = aliossMapper.selectById(1);
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String accessKey = alioss.getAccesskeyid();
        String secretKey = alioss.getAccesskeysecret();
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, secretKey);
        String bucketName = alioss.getBucket();

        try {
            // 删除文件
            ossClient.deleteObject(bucketName, fileName);
            return ResponseWrapper.markSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseWrapper.markError();
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

    @Override
    public ResponseWrapper getCode(String phone) {

        // 获取阿里云配置信息
        String accessKeyId = "LTAI5tJVcYNkMZPVZB2WMjKn";
        String accessKeySecret = "AxXbgsQSYS7V7RVbadxfrr9QvGMjM9";
        String region = "cn-hangzhou";
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret)
                .setRegionId(region);
        // 初始化客户端
        try {
            Client client = new Client(config);
            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            sendSmsRequest.setPhoneNumbers(phone);
            sendSmsRequest.setSignName("永康市道丰电子科技");
            sendSmsRequest.setTemplateCode("SMS_478075203");
            // 获取四位随机数字
            Random random = new Random();
            int code = 1000 + random.nextInt(9000);
            sendSmsRequest.setTemplateParam("{\"code\":\"" + code + "\"}");
            // 发送短信
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            System.out.println("返回结果：" + sendSmsResponse);
            if (sendSmsResponse.getBody().getCode()!= null && sendSmsResponse.getBody().getCode().equals("OK")) {
                System.out.println("短信发送成功");
                return ResponseWrapper.markSuccess(code);
            } else {
                System.out.println("短信发送失败: " + sendSmsResponse.getBody().getMessage());
                return null;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
